/*
 * CollapsibleJPanel.java
 *
 * Created on April 17, 2007, 3:17 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.jesseenglish.swingftfy.additions;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 *
 * @author english1
 */
public class CollapsibleJPanel extends JPanel {
    
    private boolean open;
    private JPanel contentsPanel;
    private JButton expander;
    private JLabel label;
    
    private JPanel toolBarPanel;
    
    /** Creates a new instance of CollapsibleJPanel */
    public CollapsibleJPanel(String title) {
        
        this.label = new JLabel(title);
        //this.contentsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.contentsPanel = new JPanel(new GridLayout(1, 1));
        this.contentsPanel.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        this.open = true;
        super.setLayout(new GridBagLayout());

        this.expander = new JButton("-");
        this.expander.addActionListener(new ExpanderActionListener());
        this.expander.setFont(new java.awt.Font("Verdana", 0, 14));
        this.expander.setAlignmentY(0.0F);
        this.expander.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        this.expander.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        this.expander.setIconTextGap(0);
        this.expander.setMargin(new java.awt.Insets(3, 3, 3, 3));
        this.expander.setMaximumSize(new java.awt.Dimension(17, 17));
        this.expander.setMinimumSize(new java.awt.Dimension(17, 17));
        this.expander.setPreferredSize(new java.awt.Dimension(17, 17));
        this.expander.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        this.expander.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        this.expander.setOpaque(true);
        this.expander.setBackground(Color.WHITE);
        this.expander.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.NONE;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.0;
        c.weighty = 0.0;
        
        super.add(this.expander, c);
        
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.NONE;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.insets = new Insets(0, 3, 0, 3);
        
        super.add(label, c);
        
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.BOTH;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.insets = new Insets(0, 0, 0, 0);
        
        this.toolBarPanel = new JPanel();
        //this.toolBarPanel.setBackground(Color.RED);
        this.toolBarPanel.setLayout(new GridLayout(1, 1));
        super.add(this.toolBarPanel, c);
        
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.BOTH;
        c.gridheight = 1;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 0.0;
        c.weighty = 0.1;
        //c.insets = new Insets(0, 5, 5, 5);
        
        super.add(this.contentsPanel, c);
    }
    
    public boolean isOpen() {
        return this.open;
    }
    
    public JLabel getLabel() {
        return this.label;
    }

    public JButton getExpander() {
        return this.expander;
    }
    
    private class ExpanderActionListener implements ActionListener {       
        public void actionPerformed(ActionEvent evt) {
            toggleView();
        }
    }
    
    public void toggleView() {
        this.open = !open;
        
        if (this.open) {
            
            GridBagConstraints c = new GridBagConstraints();
            c.anchor = GridBagConstraints.NORTHWEST;
            c.fill = GridBagConstraints.BOTH;
            c.gridheight = 1;
            c.gridwidth = 3;
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 0.0;
            c.weighty = 0.1;
            
            super.add(this.contentsPanel, c);
            this.expander.setText("-");
        }
        else {
            this.remove(this.contentsPanel);
            this.expander.setText("+");
        }

        try {
            this.getParent().validate();
            this.getParent().repaint();
        } catch (Exception err) {}
        
    }
    
    public void setToolBar(JPanel toolBar) {
        this.toolBarPanel.removeAll();
        this.toolBarPanel.add(toolBar);
    }
    
    public void setLayout(LayoutManager layoutManager) {
        if (this.contentsPanel != null)
            this.contentsPanel.setLayout(layoutManager);
    }
    
    public void setBackground(Color color) {
        super.setBackground(color);
        if (this.expander != null)
            this.expander.setBackground(color);
        if (this.contentsPanel != null) {
            this.contentsPanel.setBackground(color);
        }
        if (this.toolBarPanel != null) {
            this.toolBarPanel.setBackground(color);
            for (int i = 0; i < this.toolBarPanel.getComponentCount(); i++) {
                this.toolBarPanel.getComponent(i).setBackground(color);
            }
        }
    }
    
    public void add(Component comp, Object object, int i) {
        this.contentsPanel.add(comp, object, i);
    }
    
    public Component add(String string, Component comp) {
        return this.contentsPanel.add(string, comp);
    }
    
    public Component add(Component comp) {
        return this.contentsPanel.add(comp);
    }
    
    public void add(Component comp, Object object) {
        this.contentsPanel.add(comp, object);
    }
    
    public void add(PopupMenu popupMenu) {
        this.contentsPanel.add(popupMenu);
    }
    
    public Component add(Component comp, int i) {
        return this.contentsPanel.add(comp, i);
    }
}
